package cn.exrick.xboot.modules.Information.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.Information.entity.Cross;
import cn.exrick.xboot.modules.Information.entity.River;
import cn.exrick.xboot.modules.Information.service.ICrossService;
import cn.exrick.xboot.modules.Information.service.IriverService;
import cn.exrick.xboot.modules.base.entity.Department;
import cn.exrick.xboot.modules.base.service.DepartmentService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Lyg
 */
@Slf4j
@RestController
@Api(description = "断面信息管理接口")
@RequestMapping("/xboot/cross")
@Transactional
public class CrossController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ICrossService iCrossService;
    @Autowired
    private IriverService iriverService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Cross> get(@PathVariable Long id){

        Cross cross = iCrossService.getById(id);
        return new ResultUtil<Cross>().setData(cross);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Cross>> getAll(){

        List<Cross> list = iCrossService.list();
        return new ResultUtil<List<Cross>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Cross>> getByPage(@ModelAttribute PageVo page){

        IPage<Cross> data = iCrossService.page(PageUtil.initMpPage(page));
        for (Cross cross :data.getRecords()) {
            //行政区域赋值
            if(StrUtil.isNotBlank(cross.getWithDepartment())){
                Department department = departmentService.get(cross.getWithDepartment());
                cross.setWithDepartmentName(department.getTitle());
            }
            //所属河流名称赋值
            if(StrUtil.isNotBlank(cross.getRiverId())){
                QueryWrapper queren = new QueryWrapper();
                queren.eq("id",cross.getRiverId());
                River river = iriverService.getById(cross.getRiverId());//.get(cross.getWithDepartment());
                cross.setRiverName(river.getRiverName());
            }

        }
        return new ResultUtil<IPage<Cross>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Cross> saveOrUpdate(@ModelAttribute Cross cross){

        if(iCrossService.saveOrUpdate(cross)){
            return new ResultUtil<Cross>().setData(cross);
        }
        return new ResultUtil<Cross>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable Long[] ids){

        for(Long id : ids){
            iCrossService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
